﻿using JiaBoPrint.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 诺贝达样衣打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 诺贝达样衣打印()
        {
            InitializeComponent();
        }

        private void 诺贝达样衣打印_Load(object sender, EventArgs e)
        {
            //绑定品名下拉框
            //multiComboBox1.CheckedListBox.Items.Add("全部");
            //string sqlpm = "select distinct b.productname from [results] a left join[products] b on a.dm = b.duanma";
            //DataTable dtpm = DBHelper.ExecuteDataTable(sqlpm);
            //for (int i = 0; i < dtpm.Rows.Count; i++)
            //{
            //    multiComboBox1.CheckedListBox.Items.Add(dtpm.Rows[i]["productname"].ToString());
            //}
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //有导入页面 http://192.168.0.176:1003/views/SmallStickerCk/list_nbd.html
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();

                //http://api.satchi.com.cn/SatchiApi/GetJiaBoPrint/GetNBDYyPrint?skc=632780203057
                //http://localhost:30301/SatchiApi/GetJiaBoPrint/GetNBDYyPrint?skc=632780203057
                string json = Helper.HttpGet_query("http://api.satchi.com.cn/SatchiApi/GetJiaBoPrint/GetNBDYyPrint?skc=" + dm + "");
                NBDYyPrint yyprint = JsonConvert.DeserializeObject<NBDYyPrint>(json);//用List作为容器装Person数组

                int code = yyprint.code;
                string message = yyprint.message;
                //code-1未查到，0有数据
                if (code == -1)
                {
                    string micPath = Application.StartupPath;
                    SoundPlayer player = new SoundPlayer(micPath + @"\callvoice.wav");
                    player.Play();
                    frmError frmerror = new frmError();
                    frmerror.ShowDialog();
                }
                else
                {
                    string micPath = Application.StartupPath;
                    SoundPlayer player = new SoundPlayer(micPath + @"\ok.wav");
                    player.Play();

                    List<ListYY> listyy = yyprint.list;

                    for (int i = 0; i < listyy.Count; i++)
                    {
                        NewUsb.FindUSBPrinter();
                        for (int k = 0; k < NewUsb.USBPortCount; k++)
                        {
                            if (NewUsb.LinkUSB(k))
                            {
                                SendData2USB("SIZE 50 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                                SendData2USB("GAP 6 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                                SendData2USB("CLS\r\n");//清空缓冲区
                                SendData2USB("DENSITY 7\r\n");//打印浓度
                                SendData2USB("REFERENCE 0,0\r\n");
                                SendData2USB("TEXT 2,10,\"TSS24.BF2\",0,1,1,\"中类：\"\r\n");
                                SendData2USB("TEXT 65,10,\"TSS24.BF2\",0,1,1,\"" + listyy[i].zhonglei.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,60,\"TSS24.BF2\",0,1,1,\"货号：\"\r\n");
                                SendData2USB("TEXT 65,60,\"TSS24.BF2\",0,1,1,\"" + listyy[i].SKC.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,110,\"TSS24.BF2\",0,1,1,\"吊牌价：\"\r\n");
                                SendData2USB("TEXT 100,110,\"TSS24.BF2\",0,1,1,\"" + listyy[i].price.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,160,\"TSS24.BF2\",0,1,1,\"波段：\"\r\n");
                                SendData2USB("TEXT 100,160,\"TSS24.BF2\",0,1,1,\"" + listyy[i].boduan.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,210,\"TSS24.BF2\",0,1,1,\"入库数：\"\r\n");
                                SendData2USB("TEXT 100,210,\"TSS24.BF2\",0,1,1,\"" + listyy[i].rksl.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,260,\"TSS24.BF2\",0,1,1,\"销售数：\"\r\n");
                                SendData2USB("TEXT 100,260,\"TSS24.BF2\",0,1,1,\"" + listyy[i].salesl.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,310,\"TSS24.BF2\",0,1,1,\"产销率：\"\r\n");
                                SendData2USB("TEXT 100,310,\"TSS24.BF2\",0,1,1,\"" + listyy[i].cxl.ToString() + "\"\r\n");

                                SendData2USB("TEXT 2,360,\"TSS24.BF2\",0,1,1,\"库存数：\"\r\n");
                                SendData2USB("TEXT 100,360,\"TSS24.BF2\",0,1,1,\"" + listyy[i].kcs.ToString() + "\"\r\n");


                                SendData2USB("PRINT 1\r\n");
                                NewUsb.CloseUSBPort();

                                textBox1.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("请确定是否连接上打印机");
                            }
                        }
                    }
                }


            }
        }
    }
}
